package com.user.service;

import com.user.dao.UserRepository;
import com.user.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author zhangyan
 * @date 2022/10/4
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;

    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public List<UserInfo> findAll() {
        return userRepository.findAll();
    }

    @Override
    public UserInfo findById(Integer id) {
        Optional<UserInfo> userInfo = userRepository.findById(id);
        return userInfo.orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserInfo updateUserInfo(UserInfo userInfo) {
        return userRepository.save(userInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Integer id) {
        userRepository.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserInfo addUserInfo(UserInfo userInfo) {
        return userRepository.save(userInfo);
    }
}
